<?php

namespace App\Admin\Forms;

use App\Models\CardType;
use Dcat\Admin\Widgets\Form;

class GenerateKeysForm extends Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $cardid=$input['card_type'];
        $nums=$input['nums'];
        for($i=0;$i<$nums;$i++){
            $key=md5(uniqid(rand(), true));
            $data=[
                'card_id'=>$cardid,
                'key'=>$key,
                'status'=>0,
            ];
            \App\Models\CardNum::create($data);
        }

        return $this
				->response()
				->success('生成成功')
				->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->select('card_type','卡类')->options(CardType::pluck('name', 'id'))->required();
        $this->number('nums','数量');
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'card_type'  => 1,
            'nums' => 30,
        ];
    }
}
